using System;
using System.Windows.Forms;
using Core;
using Core.GameObjects;

namespace SpaceEdit
{
    public partial class Editor : Form, IController
    {
        private Game game;
        private float zoom;
        private readonly Timer ticker = new Timer();
        private int ticks;

        public Editor()
        {
            InitializeComponent();

            game = new Game();
            game.Paused = true;
            game.AddController(this);

            EditControl.Game = game;

            ticker.Tick += Events_Tick;
            ticker.Interval = 30;
            ticker.Enabled = true;
        }

        void Events_Tick(object sender, EventArgs e)
        {
            ticks += 1;
            game.Update(ticks);
        }

        public Game Game
        {
            get { return game; }
            set { game = value; }
        }

        public float Zoom
        {
            get { return zoom; }
            set { zoom = value; }
        }

        public void Render()
        {
            EditControl.Render();
        }

        private void playerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Player newPlayer = new Player(EditControl.Width/2, EditControl.Height/2);
            newPlayer.Name = string.Format("Object{0}", Game.Objects.Count.ToString());
            Game.AddObject(newPlayer);
        }

        private void EditControl_OnSelect(object sender, OnSelectHandlerArgs args)
        {
            propertyGrid1.SelectedObject = args.SelectedObject;
        }
    }
}